let { api } = require("./../../utils/api.js");
let { getTime } = require("./../../utils/constant.js");
let sortData = [
];
Page({
  data: {
    menu: [
      {
        name: "荤菜",
        ids: "Meat",
        dishes: [],
        checkMenu: [],
        imgUrl: "meat.png"
      },
      {
        name: "素菜",
        ids: "Vegetable",
        dishes: [],
        checkMenu: [],
        imgUrl: "vegetables.png"
      },
      {
        name: "点心",
        ids: "Snack",
        dishes: [],
        checkMenu: [],
        imgUrl: "snacks.png"
      },
      {
        name: "水果",
        ids: "Fruits",
        dishes: [],
        checkMenu: [],
        imgUrl: "fruits.png"
      }
    ],
    dishlist: [], //勾选的菜品ID
    selMenuType: 0
  },
  onLoad: function() {
    // this.getALLMenu();
  },
  onShow: function() {
    sortData = [
      [
        {
          text: "鱼",
          arr: []
        },
        {
          text: "鸡",
          arr: []
        },
        {
          text: "排骨",
          arr: []
        },
        {
          text: "蛋",
          arr: []
        },
        {
          text: "虾",
          arr: []
        },
        {
          text: "鸭",
          arr: []
        },
        {
          text: "大排",
          arr: []
        },
        {
          text: "猪",
          arr: []
        },
        {
          text: "蟹",
          arr: []
        },
        {
          text: "烧肉",
          arr: []
        },
        {
          text: "牛",
          arr: []
        },
        {
          text: "其他",
          arr: []
        }
      ],
      [
        {
          text: "菜",
          arr: []
        },
        {
          text: "蛋",
          arr: []
        },
        {
          text: "肉丝",
          arr: []
        },
        {
          text: "瓜",
          arr: []
        },
        {
          text: "土豆",
          arr: []
        },
        {
          text: "三",
          arr: []
        },
        {
          text: "茄子",
          arr: []
        },
        {
          text: "豆腐",
          arr: []
        },
        {
          text: "玉米",
          arr: []
        },
        {
          text: "土豆",
          arr: []
        },
        {
          text: "豆",
          arr: []
        },
        {
          text: "藕",
          arr: []
        },
        {
          text: "西兰花",
          arr: []
        },
        {
          text: "其他",
          arr: []
        }
      ],
      [
        {
          text: "南瓜",
          arr: []
        },
        {
          text: "饭",
          arr: []
        },
        {
          text: "粥",
          arr: []
        },
        {
          text: "面",
          arr: []
        },
        {
          text: "汤",
          arr: []
        },
        {
          text: "馒头",
          arr: []
        },
        {
          text: "其他",
          arr: []
        }
      ]
    ];
    this.getALLMenu();
  },
  // 模糊匹配
  vagueCheck(str, data) {
    let reg = new RegExp(str, "i");
    return reg.test(data);
  },
  // 菜品分类排序
  menuSort: function(allDataList) {
    let sortVal = sortData[allDataList.dishCategory - 1];
    for (let i = 0; i < sortVal.length; i++) {
      if (i === sortVal.length - 1) {
        sortVal[i].arr.push(allDataList);
      } else if (this.vagueCheck(sortVal[i].text, allDataList.dishName)) {
        sortVal[i].arr.push(allDataList);
        break;
      }
    }
  },
  // 获取所有菜品数据
  getALLMenu: function() {
    let that = this;
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: api + "getAllDish.do",
      method: "GET",
      success: function(res) {
        if (res.data.status === 1) {
          console.log("发布菜单：我刷新了！");
          let allData = res.data.data;
          let finalData = [[], [], [], []];
          for (let i = 0; i < allData.length; i++) {
            if (allData[i].dishCategory === 4) {
              finalData[3].push(allData[i]);
            } else {
              that.menuSort(allData[i]);
            }
          }
          for (let j = 0; j < sortData.length; j++) {
            for (let k = 0; k < sortData[j].length; k++) {
              finalData[j] = finalData[j].concat(sortData[j][k].arr);
            }
          }
          // let aa = "";
          // finalData[2].map((item, index) => {
          //   aa = aa + (index + 1) + `. ` + item.dishName + `  `;
          // });
          // console.log(aa);
          let obj = {
            ["menu[0].dishes"]: finalData[0],
            ["menu[1].dishes"]: finalData[1],
            ["menu[2].dishes"]: finalData[2],
            ["menu[3].dishes"]: finalData[3]
          };
          that.setData(obj);
        } else {
          wx.showToast({
            title: "获取所有菜品数据失败，请稍后重试!",
            icon: "none",
            duration: 2000
          });
        }
        wx.hideLoading();
      },
      fail: function(res) {
        wx.hideLoading();
        wx.showToast({
          title: "网络错误，请稍后重试",
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 选择菜系类型
  selTypeTab: function(e) {
    // console.log(e.currentTarget.dataset.typename);
    let { typename, scroll } = e.currentTarget.dataset;
    // if (scroll === "true") {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 1000
    });
    // }
    this.setData({
      selMenuType: typename
    });
  },
  // checkbox选择
  checkboxChange: function(e) {
    let obj = {
      ["menu[" + this.data.selMenuType + "].checkMenu"]: e.detail.value
    };
    this.setData(obj);
  },
  // 勾选菜单的时候将菜品dishId加入数组
  aadDishId: function(e) {
    let { dishlist } = this.data;
    let dishid = e.currentTarget.dataset.dishid;
    if (dishlist.indexOf(dishid) > -1) {
      dishlist.splice(dishlist.indexOf(dishid), 1);
    } else {
      dishlist.push(dishid);
    }
    // dishlist.push(e.currentTarget.dataset.dishid); wxml文件中的 data-dishId （含大写）在js文件中使用 e.currentTarget.dataset.dishid (小写调用)----即大写转小写，另外“-” 会转为驼峰写法
    this.setData({
      dishlist
    });
    // console.log(dishlist);
  },

  // 校验四大类是否都有选择
  checkDishlist: function() {
    let { menu } = this.data;
    for (let i = 0; i < menu.length; i++) {
      if (menu[i].checkMenu.length === 0) {
        wx.showToast({
          title: `请选择 ` + menu[i].name,
          icon: "none",
          duration: 3000
        });
        return false;
      }
    }
    return true;
  },
  // 发布
  submitMenu: function() {
    let { dishlist } = this.data;
    // console.log(dishlist);
    if (this.checkDishlist()) {
      wx.showModal({
        title: "",
        content: "所选菜单确认无误？",
        confirmText: "确认发布",
        success(res) {
          if (res.confirm) {
            let obj = {
              menuDate: getTime(),
              dishlist
            };
            wx.request({
              url: api + "addMenu.do",
              method: "POST",
              data: obj,
              success: function(res) {
                if (res.data.status === 1) {
                  wx.showToast({
                    title: "发布成功!",
                    icon: "success",
                    duration: 2000,
                    success: function() {
                      setTimeout(() => {
                        wx.switchTab({
                          url: "/pages/index/index"
                        });
                      }, 2000);
                    }
                  });
                } else {
                  wx.showToast({
                    title: "发布失败，请稍后重试!",
                    icon: "none",
                    duration: 2000
                  });
                }
              },
              fail: function(res) {
                wx.showToast({
                  title: "网络错误，请稍后重试",
                  icon: "none",
                  duration: 2000
                });
              }
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        }
      });
      // } else {
      //   wx.showToast({
      //     title: "请勾选菜品 !",
      //     image: "./../../images/public/tips.png",
      //     duration: 2000
      //   });
    }
  }
});
